package org.tis.tools.abf.module.om.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.om.controller.request.OmBatchDeleteRequest;
import org.tis.tools.abf.module.om.controller.request.OmPositionAppListRequest;
import org.tis.tools.abf.module.om.controller.request.OmPositionAppRequest;
import org.tis.tools.abf.module.om.entity.OmPositionApp;
import org.tis.tools.abf.module.om.service.IOmPositionAppService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * omPositionApp的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/omPositionApp")
@Validated
@Slf4j
public class OmPositionAppController extends BaseController<OmPositionApp>  {

    @Autowired
    private IOmPositionAppService omPositionAppService;

    @ApiOperation(value = "新增岗位应用列表",notes = "传入OmPositionAppRequest类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "新增岗位应用列表")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "OmPositionAppRequest",name = "传入的OmPositionAppRequest类", required = true) OmPositionAppRequest omPositionAppRequest) {
        omPositionAppService.add(omPositionAppRequest);
        return ResultVO.success("新增成功!");
    }

    @ApiOperation(value = "批量新增机构岗位应用列表",notes = "传入OmPositionAppListRequest类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "批量新增机构岗位应用列表")
    @PostMapping("/addByList")
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "OmPositionAppListRequest",name = "传入的OmPositionAppListRequest类", required = true) OmPositionAppListRequest om ){
        omPositionAppService.addList(om);
        return ResultVO.success("新增成功!");
    }


    @ApiOperation(value = "修改岗位应用列表",notes = "传入OmPositionAppRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改岗位应用列表")
    @PutMapping
    public ResultVO update(@RequestBody @Validated @ApiParam(value = "OmPositionAppRequest",name = "传入的OmPositionAppRequest类", required = true) OmPositionAppRequest omPositionAppRequest) {
        OmPositionApp omPositionAppQuery = omPositionAppService.selectById(omPositionAppRequest.getGuid());
        if (omPositionAppQuery == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        omPositionAppQuery = omPositionAppService.change(omPositionAppRequest);
        return ResultVO.success("修改成功！",omPositionAppQuery);
    }

    @ApiOperation(value = "批量新增工作组岗位应用列表",notes = "传入OmPositionAppListRequest类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "批量新增工作组岗位应用列表")
    @PostMapping("/addByGroupList")
    public ResultVO addByGroupList(@RequestBody @Validated @ApiParam(value = "OmPositionAppListRequest",name = "传入的OmPositionAppListRequest类", required = true) OmPositionAppListRequest om ){
        omPositionAppService.addList(om);
        return ResultVO.success("新增成功!");
    }


    @ApiOperation(value = "删除岗位应用列表",notes = "传入岗位应用的GUID(id)类作为参数")
    @OperateLog(type = OperateType.DELETE,desc ="删除岗位应用列表")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "岗位应用的GUID", required = true) String id) {
        OmPositionApp omPositionApp = omPositionAppService.selectById(id);
        if (omPositionApp == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        omPositionAppService.deleteById(id);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "根据岗位id和应用id删除机构下岗位应用列表",notes = "传入岗位GUID(guidPosition)和应用GUID(guidApp)类作为参数")
    @OperateLog(type = OperateType.DELETE,desc ="根据岗位id和应用id删除机构下岗位应用列表")
    @DeleteMapping("/delete/{guidApp}/{guidPosition}")
    public ResultVO deleteByPositionAndApp(@PathVariable @NotBlank(message = "应用GUID不能为空") @ApiParam(value = "guidApp",name = "应用的GUID", required = true) String guidApp,@PathVariable @NotBlank(message = "岗位GUID不能为空") @ApiParam(value = "guidPosition",name = "岗位的GUID", required = true) String guidPosition) {
        omPositionAppService.deleteByPositionAndApp(guidApp,guidPosition);
        return ResultVO.success("删除成功！");
    }

    @ApiOperation(value = "批量删除机构下岗位应用列表",notes = "传入岗位GUID(guid)和应用GUID组(deleteList)类作为参数")
    @OperateLog(type = OperateType.DELETE,desc ="批量删除机构下岗位应用列表")
    @PostMapping("/batchDelete")
    public ResultVO batchDelete(@RequestBody @Validated @ApiParam(value = "OmBatchDeleteRequest",name = "传入参数OmBatchDeleteRequest", required = true) OmBatchDeleteRequest omBatchDeleteRequest) {
        omPositionAppService.batchDelete(omBatchDeleteRequest);
        return ResultVO.success("删除成功！");
    }


    @ApiOperation(value = "根据ID查询岗位应用",notes = "传入岗位应用的GUID(id)类作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "岗位应用的GUID", required = true) String id) {
        OmPositionApp omPositionApp = omPositionAppService.selectById(id);
        if (omPositionApp == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", omPositionApp);
    }


    @ApiOperation(value = "分页查询所有岗位应用",notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息", required = true) SmartPage<OmPositionApp> page) {
        return  ResultVO.success("查询成功", omPositionAppService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }


    @ApiOperation(value = "查询岗位已经有的应用权限",notes = "传入分页信息和岗位GUID作为参数")
    @PostMapping("/listByPosition/{guidPosition}")
    public ResultVO listByPosition(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息", required = true) SmartPage<OmPositionApp> page,@PathVariable @NotBlank(message = "岗位guidPosition不能为空") @ApiParam(value = "guidPosition",name = "岗位GUID", required = true) String guidPosition){
        return ResultVO.success("查询成功",omPositionAppService.queryAppByPosition(page.getPageObj(),page.getConditionWrapper(), guidPosition));
    }


    @ApiOperation(value = "根据岗位ID,查询不在该岗位下的应用",notes = "传入岗位GUID(id)作为参数")
    @GetMapping("/appNotInPosition/{id}")
    public ResultVO listNotInPosition(@PathVariable @NotBlank(message = "岗位id不能为空") @ApiParam(value = "id",name = "岗位GUID", required = true) String id){
        return ResultVO.success("查询成功",omPositionAppService.queryNotInPosition(id));
    }


    @ApiOperation(value = "根据岗位id和应用id删除工作组下岗位应用列表",notes = "传入岗位GUID(guidPosition)和应用GUID(guidApp)类作为参数")
    @OperateLog(type = OperateType.DELETE,desc ="根据岗位id和应用id删除工作组下岗位应用列表")
    @DeleteMapping("/{guidApp}/deleteByGroup/{guidPosition}")
    public ResultVO deleteByGroup(@PathVariable @NotBlank(message = "应用GUID不能为空") @ApiParam(value = "guidApp",name = "应用的GUID", required = true) String guidApp,@PathVariable @NotBlank(message = "岗位GUID不能为空") @ApiParam(value = "guidPosition",name = "岗位的GUID", required = true) String guidPosition) {
        omPositionAppService.deleteByPositionAndApp(guidApp,guidPosition);
        return ResultVO.success("删除成功！");
    }

    @ApiOperation(value = "批量删除工作组下岗位应用列表",notes = "传入岗位GUID(guid)和应用GUID组(deleteList)类作为参数")
    @OperateLog(type = OperateType.DELETE,desc ="批量删除工作组下岗位应用列表")
    @PostMapping("/batchDeleteByGroup")
    public ResultVO batchDeleteByGroup(@RequestBody @Validated @ApiParam(value = "OmBatchDeleteRequest",name = "传入参数OmBatchDeleteRequest", required = true) OmBatchDeleteRequest omBatchDeleteRequest) {
        omPositionAppService.batchDelete(omBatchDeleteRequest);
        return ResultVO.success("删除成功！");
    }

    
}

